<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">

      <el-form-item label="品牌:">
       <el-select v-model="queryParams.brandCode" placeholder="请选择品牌" clearable size="mini" >
          <el-option v-for="boption in options.brands" :key="boption.brandCode" :label="boption.brandName" :value="boption.brandCode"  />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商:">
        <el-select v-model="queryParams.supplierCode" placeholder="请选择供应商" clearable size="small">
          <el-option v-for="option in options.supplierIdOptions" :key="option.supplierCode" :label="option.supplierName" :value="option.supplierName" />
        </el-select>
      </el-form-item>

      <el-form-item label="店铺:">
       <el-select v-model="queryParams.storeId" placeholder="请选择店铺编号" clearable size="mini">
         <el-option v-for="option in options.storeOptions" :key="option.storeCode" :label="option.storeName" :value="option.supplierCode" />
       </el-select>
      </el-form-item>
    <!--  <el-form-item label="供应商类型:">
        <el-select v-model="queryParams.supplierType" placeholder="请选择供应商类型" clearable size="small">
          <el-option v-for="option in options.supplierTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" height="550" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column label="供应商编码" width="95" align="center" prop="supplierCode" />
      <el-table-column label="品牌编码" align="center" prop="brandCode" />
     <el-table-column label="品牌名称" align="center" prop="brandName" />
     <el-table-column label="店鋪編碼" align="center" prop="storeId" />
     <el-table-column label="品牌首字母" align="center" prop="letter" />
     <el-table-column label="授权开始日期" align="center" prop="authStartDate" />
     <el-table-column label="授权结束日期" align="center" prop="authExpDate" />
     <el-table-column label="品牌Logo" align="center" prop="logo">
       <template slot-scope="scope">
         <img style="height: 40px" :src="scope.row.logo">
       </template>
     </el-table-column>
     <el-table-column label="排序" align="center" prop="sort" />
     <el-table-column label="创建时间" align="center" prop="createTime" width="180">
       <template slot-scope="scope">
         <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
       </template>
     </el-table-column>
      <el-table-column label="操作" align="center"  fixed="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:supplier:update']" size="mini" type="text" @click="handleView(scope.row)">查看</el-button>
          <!--<el-dropdown type="text" split-button>
             <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-hasPermission="['basic:supplier:update']" size="mini" type="primary" icon="el-icon-edit"  @click.native="handleEdit(scope.row)">修改</el-dropdown-item>
                <el-dropdown-item v-hasPermission="['basic:supplier:delete']" size="mini" type="primary" icon="el-icon-delete" @click.native="handleRemove(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
             <!--      <el-button v-hasPermission="['basic:supplier:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
    -->    </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->

    <!-- 添加/修改对话框 -->
    <el-dialog :title="supplierFormDialog.title" :visible.sync="supplierFormDialog.visible" :close-on-click-modal="false" width="580px" append-to-body>
      <el-form ref="supplierForm" :model="supplierFormData" :rules="supplierFormRules" label-width="100px">
       <el-row :gutter="24" class="form-item-group">
         <el-col :span="12">
       <el-form-item label="供应商编码" prop="supplierCode">
          <el-input v-model="supplierFormData.supplierCode" placeholder="请输入供应商编码" @change="handleCheckCode"/>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="supplierFormData.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        </el-col>
         </el-row>
         <el-row :gutter="24" class="form-item-group">
           <el-col :span="12">
        <el-form-item label="统一社安号" prop="bizLicenceNo">
          <el-input v-model="supplierFormData.bizLicenceNo" placeholder="请输入统一社安号" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="supplierFormData.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        </el-col>
         </el-row>
         <el-row :gutter="24" class="form-item-group">
           <el-col :span="12">
        <el-form-item label="银行账户" prop="bankAccount">
         <el-input v-model="supplierFormData.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
      <!--  <el-form-item label="供应商类型" prop="supplierType">
          <el-select v-model="supplierFormData.supplierType" style="width: 440px" clearable placeholder="请选择供应商类型">
            <el-option v-for="option in options.supplierTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item> -->

        <el-form-item label="法人" prop="owner">
          <el-input v-model="supplierFormData.owner" placeholder="请输入法人" />
        </el-form-item>
        </el-col>
         </el-row>
         <el-row :gutter="24" class="form-item-group">
           <el-col :span="12">
        <el-form-item label="注册资本" prop="capital">
          <el-input v-model="supplierFormData.capital" placeholder="请输入注册资本" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="英文名" prop="englishName">
          <el-input v-model="supplierFormData.englishName" placeholder="请输入英文名" />
        </el-form-item>
        </el-col>
         </el-row>
         <el-row :gutter="24" class="form-item-group">
           <el-col :span="12">
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="supplierFormData.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input v-model="supplierFormData.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        </el-col>
         </el-row>
         <el-row :gutter="24" class="form-item-group">
           <el-col :span="12">
        <el-form-item label="联系人邮件" prop="contactEmail">
          <el-input v-model="supplierFormData.contactEmail" placeholder="请输入联系人邮件" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="开业时间：" prop="openDate">
              <el-date-picker
                v-model="supplierFormData.openDate"
                type="datetime"
                placeholder="开业时间"
              />
               </el-form-item>
              </el-col>
               </el-row>
               <el-row :gutter="24" class="form-item-group">
                 <el-col :span="12">

           <!-- <el-form-item label="入驻时间：" prop="enrollDate">
              <el-date-picker
                v-model="customerFormData.enrollDate"
                type="datetime"
                placeholder="入驻时间"
              />
            </el-form-item> -->
        <el-form-item label="微信号" prop="wechatId">
             <el-input v-model="supplierFormData.wechatId" placeholder="微信号" />
            </el-form-item>
            <el-form-item label="联系人微信号" prop="contactWechatId">
             <el-input v-model="supplierFormData.contactWechatId" placeholder="联系人微信号" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
           <el-form-item label="网站地址" prop="webSite">
            <el-input v-model="supplierFormData.webSite" placeholder="网站地址" />
           </el-form-item>
           </el-col>
            </el-row>
            <el-row :gutter="24" class="form-item-group">
              <el-col :span="12">
           <el-form-item label="入驻到期日" prop="supplierContractExpDate">
             <el-date-picker
               v-model="supplierFormData.supplierContractExpDate"
               type="date" value-format="yyyy-MM-dd"
               placeholder="入驻到期日"
             />
           </el-form-item>
           </el-col>
           <el-col :span="12">
          <el-form-item label="资质文件一" prop="owner">
            <el-input v-model="supplierFormData.docOne" placeholder="请输入资质文件一" />
          </el-form-item>
          </el-col>
           </el-row>
           <el-row :gutter="24" class="form-item-group">
             <el-col :span="12">
          <el-form-item label="资质文件二" prop="owner">
            <el-input v-model="supplierFormData.docTwo" placeholder="资质文件二" />
          </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="资质文件三" prop="owner">
            <el-input v-model="supplierFormData.docThree" placeholder="资质文件三" />
          </el-form-item>
          </el-col>
           </el-row>
           <el-row :gutter="24" class="form-item-group">
             <el-col :span="12">
          <el-form-item label="资质文件四" prop="owner">
            <el-input v-model="supplierFormData.docFour" placeholder="请输入资质文件四" />
          </el-form-item>
          </el-col>
           </el-row>
       </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSupplierFormDialog">确 定</el-button>
        <el-button @click="resetSupplierFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BrandApi from '@/api/goods/BrandApi'
import DictValueApi from '@/api/system/DictValueApi'
import StoreApi from '@/api/basic/StoreApi'
import SupplierApi from '@/api/basic/SupplierApi'
const defaultSupplierFormData = {
  supplierCode: null,
  supplierName: null,
  supplierFullName: null,
  supplierType: null,
  supplierDesc: null,
  contactName: null,
  contactPhone: null,
  supplierContractExpDate: null,
  contactEmail: null
}
export default {
  name: 'SupplierList',
  data() {
    return {
      options: {
        supplierTypeOptions: [], // 供应商类型字典
        supplierIdOptions: [], // 供应商编号字典
        storeOptions: [],
        brands: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        storeId: null,
        brandName: null,
        brandCode: null,
        supplierCode: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      supplierFormDialog: {
        title: '',
        visible: false
      },
      supplierFormData: Object.assign({}, defaultSupplierFormData),
      supplierFormRules: {
        supplierCode: [{ required: true, message: '供应商编码不能为空', trigger: 'blur' }],
        supplierContractExpDate: [{ required: true, message: '合作期限不能为空', trigger: 'blur' }],
        supplierName: [{ required: true, message: '供应商名称不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
       let  that = this;
   StoreApi.list().then(response => {
        that.options.storeOptions = response.data
      })
      SupplierApi.list().then(response => {
        that.options.supplierIdOptions = response.data
      })
      BrandApi.list().then(response => {
        that.options.brands = response.data
      })
    },
    handleAdd() {
      this.openSupplierFormDialog()
    },
    handleView(row) {
      this.$router.push({ path: '/supplier/view/' + row.id })
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SupplierApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SupplierApi.logicalDelete(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openSupplierFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      BrandApi.authExppage(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SupplierApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openSupplierFormDialog(id) {
      this.resetSupplierFormDialog()
      this.supplierFormDialog.visible = true
      if (id) {
        this.supplierFormDialog.title = '供应商-修改'
        SupplierApi.get(id).then(response => {
          this.supplierFormData = {
            ...this.supplierFormData,
            ...response.data
          }
        })
      } else {
        this.supplierFormDialog.title = '供应商-添加'
      }
    },
    confirmSupplierFormDialog() {
      this.$refs['supplierForm'].validate(valid => {
        if (valid) {
          if (this.supplierFormData.id) {
            SupplierApi.edit(this.supplierFormData).then(response => {
              this.$message.success('修改成功')
              this.supplierFormDialog.visible = false
              this.handleQuery()
            })
          } else {
            SupplierApi.add(this.supplierFormData).then(response => {
              this.$message.success('添加成功')
              this.supplierFormDialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetSupplierFormDialog() {
      this.supplierFormDialog = {
        title: '',
        visible: false
      }
      this.supplierFormData = Object.assign({}, defaultSupplierFormData)
      this.resetForm('supplierForm')
    },
    // 供应商类型字典
    supplierTypeFormat(row) {
      return this.$common.getDictValueName(this.options.supplierTypeOptions, row.supplierType)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    handleCheckCode() {
      const params = {
        id: this.supplierFormData.id,
        supplierCode: this.supplierFormData.supplierCode
      }
      SupplierApi.checkCode(params).then(response => {
        if (!response.data) {
          this.$message.error('供应商编码重复')
        }
      })
    },
    resetQuery() {
      this.page.current = 1
     this.queryParams = {
       supplierCode: null,
       supplierName: null,
       isValid: this.$constant.Y
     }

      this.handleQuery()
    }
  }
}
</script>
